package com.inossem.warehousestorage_inossem.adapter.inventory;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.TextView;

import com.inossem.utils.base.Utils;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.utils.log.LogUtils;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.inventory.InventoryOverViewActivity;
import com.inossem.warehousestorage_inossem.bean.inventory.InventoryAreaBean;

import java.util.ArrayList;

/**
 * Created by cjz on 2017/6/6.
 */

public class InventoryOverviewAdapter extends BaseAdapter {

    private InventoryOverViewActivity mContext;
    private LayoutInflater inflater;
    private InventoryAreaBean bean;
    private EditText input;
    private ImageView search;

    //行展开list
    private ArrayList<Boolean> expantList;

    public InventoryOverviewAdapter(InventoryOverViewActivity context, InventoryAreaBean bean) {
        mContext = context;
        inflater = LayoutInflater.from(context);
        this.bean = bean;
        expantList = new ArrayList<Boolean>();
        for (InventoryAreaBean.AreaListBean alb :
                this.bean.getBody()) {
            expantList.add(false);
        }
    }

    public void setBean(InventoryAreaBean bean) {
        this.bean = bean;
    }

    @Override
    public int getCount() {
        return this.bean.getBody().size() + 1;
    }

    @Override
    public Object getItem(int position) {
        return bean.getBody().get(position - 1);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public void doSeach(String no) {
        if (!TextUtils.isEmpty(no)) {
            input.setText(no);
            LogUtils.i("no:" + no);
            //查找仓位架的位置
            int pidx = findIndex(no);
            if (pidx >= 0) {
                //打开仓架
                ViewHolder vh = (ViewHolder) mContext.getList().getChildAt(pidx + 1).getTag();
                expand(vh, pidx + 1);
            } else {
                if (no.contains("-")) {
                    pidx = findIndex(no.replaceFirst("-", " "));
                    if (pidx >= 0) {
                        //打开仓架
                        ViewHolder vh = (ViewHolder) mContext.getList().getChildAt(pidx + 1).getTag();
                        expand(vh, pidx + 1);
                    } else {
                        pidx = findIndex(decryptQRCodePosition(no));
                        if (pidx >= 0) {
                            //打开仓架
                            ViewHolder vh = (ViewHolder) mContext.getList().getChildAt(pidx + 1).getTag();
                            expand(vh, pidx + 1);
                        } else {
                            Utils.middleToast(mContext, mContext.getResources().getString(R.string.activity_inventory_overview_no_designated_position_area_was_found));
                        }
                    }
                } else {
                    Utils.middleToast(mContext, mContext.getResources().getString(R.string.activity_inventory_overview_no_designated_position_area_was_found));
                }
            }
        } else {
            Utils.middleToast(mContext, mContext.getResources().getString(R.string.activity_inventory_overview_the_position_number_is_not_empty));
        }
    }

    private String decryptQRCodePosition(String position) {
        String result;
        if (position.contains("-")) {
            String area = position.substring(0, position.lastIndexOf("-"));
            if (area.contains("-")) {
                result = area.replaceFirst("-", " ");
            } else {
                result = area;
            }
        } else {
            result = position;
        }
        return result;
    }

    /**
     * 查找仓位架的位置
     *
     * @param an
     * @return
     */
    private int findIndex(String an) {
        int positionIndex = -1;
        for (int i = 0; i < bean.getBody().size(); i++) {
            InventoryAreaBean.AreaListBean alb = bean.getBody().get(i);
            if (an.equals(alb.getPrefix())) {
                positionIndex = i;
            }
        }
        return positionIndex;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (position == 0) {
            convertView = inflater.inflate(R.layout.view_inventory_overview_first_item, null);
            input = (EditText) convertView.findViewById(R.id.input);
            search = (ImageView) convertView.findViewById(R.id.search);

            search.setOnClickListener(new NoDoubleClickListener() {
                @Override
                public void onNoDoubleClick(View v) {
                    Utils.closeSoftKeyboard(mContext, search);
//                    String no = input.getText().toString();
//                    input.setText("");
                    doSeach(input.getText().toString());
                }
            });

//            input.addTextChangedListener(new QRCodeEditTextWatcher(input) {
//                @Override
//                public void onScan(String text) {
//                    Utils.closeSoftKeyboard(mContext, input);
//                    String no = input.getText().toString();
//                    input.setText("");
//                    doSeach(no);
//                }
//            });
        } else {
            ViewHolder viewHolder;
            if (convertView == null || (convertView != null && convertView.getTag() == null)) {
                convertView = inflater.inflate(R.layout.view_inventory_overview, null);
                viewHolder = new ViewHolder();
                //初始化控件
                initView(viewHolder, convertView);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            //初始化数据
            initData(mContext, viewHolder, convertView, position);
        }


        return convertView;
    }


    private void initView(ViewHolder viewHolder, View view) {
        viewHolder.gview = (GridView) view.findViewById(R.id.gview);
        viewHolder.rowsTotal = (TextView) view.findViewById(R.id.rowsTotal);
        viewHolder.no = (TextView) view.findViewById(R.id.no);
    }

    private void initData(Context context, ViewHolder viewHolder, View view, int position) {
        InventoryAreaBean.AreaListBean listBean = (InventoryAreaBean.AreaListBean) getItem(position);

//        if(VOUCHER_TYPE_1.equals(voucherType)){
        viewHolder.rowsTotal.setText(listBean.getCurrent() + "/" + listBean.getTotal());
//        }

        viewHolder.no.setText(listBean.getPrefix());

        ListAdapter gadp = new InventoryOverviewCellAdapter(mContext, listBean);
        viewHolder.gview.setAdapter(gadp);

        //根据是否展开标识，显示gview
        if (expantList.get(position - 1)) {
            viewHolder.gview.setVisibility(View.VISIBLE);
        } else {
            viewHolder.gview.setVisibility(View.GONE);
        }

        //事件处理
        view.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {

                if (viewHolder.gview.getVisibility() == View.VISIBLE) {
                    viewHolder.gview.setVisibility(View.GONE);
                    expantList.set(position - 1, false);
                } else {
                    expand(viewHolder, position);
                }
            }
        });
    }

    private void expand(ViewHolder viewHolder, int position) {
        viewHolder.gview.setVisibility(View.VISIBLE);
        expantList.set(position - 1, true);
        mContext.getList().smoothScrollToPositionFromTop(position, 0);
    }

    private class ViewHolder {
        private TextView rowsTotal, no;
        private GridView gview;
    }

}
